import { defineComponent, watch, shallowReactive, ref } from 'vue'
import moment from 'moment'
import * as echarts from 'echarts'
// 声明类型
const PropsType = {
  data: {
    type: Object,
    default: () => ({
			list:[],
			legend:[],
      color:'rgba(50, 216, 205, 1)',
      background:'rgba(255, 255, 255, 0.2)'
    }),
    require: true
  }
} as const



// 定义主体
export default defineComponent({
  props: PropsType,

  setup(props) {
    // 定义 ref
    const chartRef = ref()

    // 配置项
    let options = {}

    watch(
      () => props.data,
      (val: any) => {
        //
        options = {
          legend: {
            data: val.legend,
            textStyle: {
              align: 'right',
            },
            top: '2%',
            right: '2%',
          },

          tooltip: {
            trigger: 'axis',
          },

          grid: {
            top: '20%',
            left: '8%',
            right: '10%',
            bottom: '13%',
          },
          xAxis: [
            {
              type: 'category',
              axisLine: {
                show: true,
              },
              splitArea: {
                // show: true,
                color: '#f00',
                lineStyle: {
                  color: '#f00',
                },
              },
              splitLine: {
                show: false,
              },
              axisLabel: {
                margin: 10,
              },
              boundaryGap: false,
              data: val.list?val.list.map(item=>moment(item.monitorTime).format('YYYY-MM-DD')):[],
            },
          ],

          yAxis: [
            {
              type: 'value',
              name:'单位:'+val.unit,
              // min: 0,
              // max: 140,
              splitNumber: 4,
              splitLine: {
                show: true,
                lineStyle: {
                  color: '#A1A7B3',
                  type: 'dashed'
                }
              },
              axisLine: {
                show: false,
              },
              axisLabel: {
                show: true,
                // margin: 20,
                formatter: '{value}',
              },
              axisTick: {
                show: false,
              },
            },
          ],
          series: {
            name: '',
            type: 'line',
            smooth: true,
            symbolSize: 5,
            showSymbol: true,
            itemStyle: {
              normal: {
                color: val.color,
                lineStyle: {
                  color: val.color,
                  width: 2
                },
              }
            },
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: val.color
                  },
                  {
                    offset: 1,
                    color: val.background
                  }
                ], false),
              }
            },
            data: val.list?val.list.map(item=>item.dataValue):[],
          }
        }
        // 手动触发更新
        if (chartRef.value) {
          // 通过初始化参数打入数据
          chartRef.value.initChart(options,true)
        }
      },
      {
        immediate: true,
        deep: true
      }
    )

    return () => {
      const height = "160px"
      const width = "432px"

      return <div>
        <echart ref={chartRef} class="mr-auto" options={options} height={height} width={width} />
      </div>
    }
  }
})

